package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.CategoryiesReponse;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.NavListResponse;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.impl.IHomeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/17 17:33
 */
@RestController
@RequestMapping("/shopping")
public class ChyShoppingController {
    // 必须叫 service 包, 也注册的容器里; 即使用注解@ComponentScan;
    @Autowired
    IHomeService iHomeService;
    /**
     * 不接受参数
     * @return
     */
    @GetMapping("/homepage")
    public ResponseData homepage(){
        System.out.println("访问 homepage");
        HomePageResponse homepage = iHomeService.homepage();
        homepage.setCode("200");
        homepage.setMsg("获取该接口成功");
        return new ResponseUtil().setData(homepage.getPanelContentItemDtos()," /shopping/homepage");
    }

    /**
     * GET 请求, 无参数;
     * @return
     */
    @GetMapping("/navigation")
    public ResponseData navigation(){

        NavListResponse navListResponse =    iHomeService.navigation();
        // navListResponse.setCode("200");
        navListResponse.setCode("200");

        navListResponse.setMsg("/shopping/navigation");
        return  new ResponseUtil().setData(navListResponse.getPannelContentDtos(),"/shopping/navigation");
    }

    /**
     * GET 请求, 无参数;
     * @return
     */
    @GetMapping("/categories")
    public ResponseData categories(String sort){
        System.out.println("访问 categories");
        if (sort==null && sort=="") {
            sort = "1";
        }
        CategoryiesReponse categoryiesReponse    =  iHomeService.categories(sort);

        /**
         * 为保持兼容; 返回categoryiesReponse.getProductCateDtos(); 而不是返回categoryiesReponse
         */
        return new ResponseUtil().setData(categoryiesReponse.getProductCateDtos(),"/shopping/categroies");

    }
}
